package com.lute.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


import com.lute.model.CompanyCategory;
import com.lute.model.CompanyHoursCategory;
import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeEntry;
import com.lute.model.PositionName;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class LuteMonthlySummaryReport extends LuteAbstractFactoryReport {
	
	private String creativeHoursPercentage;
	private String sickLeave;
	private String vacation;
	private String workForClient;
	private String hoursTotal;
	private String creativeHoursTotal;
	
	public LuteMonthlySummaryReport() {}
	
	public LuteMonthlySummaryReport(String reportType, String generatedOn, EmployeeAccountingPeriod period, String from) {
		
		super(reportType,generatedOn, period, from);
	}
	
	public String getCreativeHoursPercentage() {
		return creativeHoursPercentage;
	}

	public void setCreativeHoursPercentage(String creativeHoursPercentage) {
		this.creativeHoursPercentage = creativeHoursPercentage;
	}
	
	public String getSickLeave() {
		return sickLeave;
	}

	public void setSickLeave(String sickLeave) {
		this.sickLeave = sickLeave;
	}

	public String getVacation() {
		return vacation;
	}

	public void setVacation(String vacation) {
		this.vacation = vacation;
	}

	public String getWorkForClient() {
		return workForClient;
	}

	public void setWorkForClient(String workForClient) {
		this.workForClient = workForClient;
	}
	
	public String getHoursTotal() {
		return hoursTotal;
	}

	public void setHoursTotal(String hoursTotal) {
		this.hoursTotal = hoursTotal;
	}

	public String getCreativeHoursTotal() {
		return creativeHoursTotal;
	}

	public void setCreativeHoursTotal(String creativeHoursTotal) {
		this.creativeHoursTotal = creativeHoursTotal;
	}
	
	
	@Override
	public JRDataSource getDataSource(List<Integer> employeeIds, List<String> froms) {
		List<LuteMonthlySummaryReport> summary = new ArrayList<LuteMonthlySummaryReport>();		
		List<GregorianCalendar> cals = new ArrayList<GregorianCalendar>();
		
		for(String cal: froms) {
			String tab[] = cal.split("-");
			cals.add(new GregorianCalendar(Integer.parseInt(tab[0]),Integer.parseInt(tab[1])-1,Integer.parseInt(tab[2])));
		}
		
		SimpleDateFormat dateFromater = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		
		for(GregorianCalendar cal: cals) {
			
			List<EmployeeAccountingPeriod> employeePeriods = EmployeeAccountingPeriod.getEmployeeAccountingPeriodsFromDB(employeeIds, cal);
			for(EmployeeAccountingPeriod period: employeePeriods) {
					float  sickLeave = 0;
					float  vacation = 0;
					float  workForClient = 0;
					float hoursTotal = 0;
					float creativeHoursTotal = 0;
					
					LuteMonthlySummaryReport report = new LuteMonthlySummaryReport(LuteReportName.APPROVER_MONTHLY_SUMMARY_REPORT.toString(),
																	 dateFormat.format(date), period, dateFromater.format(cal.getTime()));
					
					creativeHoursPercentage = "n/a";
					if(period.getEmployee() == null) {
						if(!(period.getApprover().getApproverProfile().getPositions().getName().equals(PositionName.SUPPORT.toString()))) {
							creativeHoursPercentage = period.getApprover().getApproverProfile().
									   getPositions().getCreativity_percentage().toString();
						}
					} else {
						if(!(period.getEmployee().getEmployeeProfil().getPositions().getName().equals(PositionName.SUPPORT.toString()))) {
							creativeHoursPercentage = period.getEmployee().getEmployeeProfil().
						               getPositions().getCreativity_percentage().toString();
						}
					}
					
					List<EmployeeEntry> entries = period.getEmployeeEntries();
					for(EmployeeEntry entry: entries) {
						if(entry.getCompanyCategory().getName().equals(CompanyHoursCategory.SICK_LEAVE.toString())) {
							sickLeave += entry.getHours();
						}
						else if(entry.getCompanyCategory().getName().equals(CompanyHoursCategory.VACATION.toString())) {
							vacation += entry.getHours();
						} 
						else if(entry.getCompanyCategory().getName().equals(CompanyHoursCategory.WORK_CLIENT.toString())) {
							workForClient += entry.getHours();
						} else {
							/* do nothing */
						}
					}
					hoursTotal += (sickLeave + vacation + workForClient);
					if(!(creativeHoursPercentage.equals("n/a"))) {
						creativeHoursTotal = (workForClient * (Float.parseFloat(creativeHoursPercentage)))/100;
						if( ((int)(creativeHoursTotal + 0.5)) > (int)creativeHoursTotal ) {
							creativeHoursTotal = ((int)creativeHoursTotal) + 1;
						} else {
							creativeHoursTotal = (int)creativeHoursTotal;
						}
					} else {
						creativeHoursTotal = 0;
					}
					
					report.setSickLeave(Float.toString(sickLeave));
					report.setVacation(Float.toString(vacation));
					report.setWorkForClient(Float.toString(workForClient));
					report.setCreativeHoursPercentage(creativeHoursPercentage);
					report.setCreativeHoursTotal(Float.toString(creativeHoursTotal));
					report.setHoursTotal(Float.toString(hoursTotal));
					summary.add(report);
			}
		}
		
		JRDataSource ds = new JRBeanCollectionDataSource(summary);
		
		return ds;
	}
	
}
